package com.itheima.socket.udp.demo04;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * 使用MulticastSocket发送数据
 */
public class SendDemo01 {

    public static void main(String[] args) throws IOException {

        /**
         * 步骤：
         * 1. 创建MulticastSocket对象(无参构造方法)
         * 2. 创建DatagramPacket数据包对象(指定一个组播地址：224.0.0.1)
         * 3. 调用MulticastSocket发送数据
         * 4. 释放资源
         */
        // 创建MulticastSocket对象
        MulticastSocket multicastSocket = new MulticastSocket() ;

        // 创建DatagramPacket数据包对象: public DatagramPacket(byte buf[], int length,InetAddress address, int port)
        String s = "你好" ;
        byte[] bytes = s.getBytes("UTF-8");
        int length = bytes.length;
        InetAddress inetAddress = InetAddress.getByName("224.0.0.1");   // 224.0.0.0~239.255.255.255
        int port = 9999 ;
        DatagramPacket datagramPacket = new DatagramPacket(bytes , length , inetAddress , port) ;

        // 调用MulticastSocket发送数据
        multicastSocket.send(datagramPacket);

        // 释放资源
        multicastSocket.close();


    }

}
